﻿using Prism.Navigation.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Ku.AppCore.Regions
{
    public class TabControlRegionAdapter : RegionAdapterBase<HandyControl.Controls.TabControl>
    {
        public TabControlRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, HandyControl.Controls.TabControl regionTarget)
        {
            var views = new ObservableCollection<object>();

            region.Views.CollectionChanged += (s, e) =>
            {
                Debug.WriteLine($"TabControl--Adapt,事件={e.Action}");
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (var view in e.NewItems)
                    {
                        views.Add(view);
                    }
                }

                // Handle Remove action
                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (var view in e.OldItems)
                    {
                        views.Remove(view);
                    }
                }
            };

            regionTarget.ItemsSource = views;
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
}
